package com.coderknock.spring.boot.mybatis.mapper;

import com.coderknock.spring.boot.mybatis.model.Article;
import com.coderknock.spring.boot.mybatis.model.ArticleExample;
import com.coderknock.spring.boot.mybatis.typehandle.ArticleTypeHandle;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.Date;
import java.util.List;

//Mapper将 ArticleMapper 声明为一个Mapper接口
@Mapper
public interface ArticleMapper {
    @SelectProvider(type = ArticleSqlProvider.class, method = "countByExample")
    long countByExample(ArticleExample example);

    @DeleteProvider(type = ArticleSqlProvider.class, method = "deleteByExample")
    int deleteByExample(ArticleExample example);

    @Insert({
            "insert into article (id, author, ",
            "permission, context_path, ",
            "url, status, intro, ",
            "modify_time, create_time, ",
            "title_text, pv, dislike_num, ",
            "like_num, article_type)",
            "values (#{id,jdbcType=INTEGER}, #{author,jdbcType=INTEGER}, ",
            "#{permission,jdbcType=BIT}, #{contextPath,jdbcType=VARCHAR}, ",
            "#{url,jdbcType=VARCHAR}, #{status,jdbcType=BIT}, #{intro,jdbcType=VARCHAR}, ",
            "#{modifyTime,jdbcType=TIMESTAMP}, #{createTime,jdbcType=TIMESTAMP}, ",
            "#{titleText,jdbcType=VARCHAR}, #{pv,jdbcType=INTEGER}, #{dislikeNum,jdbcType=INTEGER}, ",
            "#{likeNum,jdbcType=INTEGER}, #{articleType,jdbcType=BIT})"
    })
    int insert(Article record);

    @InsertProvider(type = ArticleSqlProvider.class, method = "insertSelective")
    int insertSelective(Article record);

    @SelectProvider(type = ArticleSqlProvider.class, method = "selectByExample")
    @ConstructorArgs({
            @Arg(column = "id", javaType = Integer.class, jdbcType = JdbcType.INTEGER),
            @Arg(column = "author", javaType = Integer.class, jdbcType = JdbcType.INTEGER),
            @Arg(column = "permission", javaType = Boolean.class, jdbcType = JdbcType.BIT),
            @Arg(column = "context_path", javaType = String.class, jdbcType = JdbcType.VARCHAR),
            @Arg(column = "url", javaType = String.class, jdbcType = JdbcType.VARCHAR),
            @Arg(column = "status", javaType = Boolean.class, jdbcType = JdbcType.BIT),
            @Arg(column = "intro", javaType = String.class, jdbcType = JdbcType.VARCHAR),
            @Arg(column = "modify_time", javaType = Date.class, jdbcType = JdbcType.TIMESTAMP),
            @Arg(column = "create_time", javaType = Date.class, jdbcType = JdbcType.TIMESTAMP),
            @Arg(column = "title_text", javaType = String.class, jdbcType = JdbcType.VARCHAR),
            @Arg(column = "pv", javaType = Integer.class, jdbcType = JdbcType.INTEGER),
            @Arg(column = "dislike_num", javaType = Integer.class, jdbcType = JdbcType.INTEGER),
            @Arg(column = "like_num", javaType = Integer.class, jdbcType = JdbcType.INTEGER),
            @Arg(column = "article_type", javaType = String.class, jdbcType = JdbcType.BIT)
    })
    List<Article> selectByExample(ArticleExample example);

    @UpdateProvider(type = ArticleSqlProvider.class, method = "updateByExampleSelective")
    int updateByExampleSelective(@Param("record") Article record, @Param("example") ArticleExample example);

    @UpdateProvider(type = ArticleSqlProvider.class, method = "updateByExample")
    int updateByExample(@Param("record") Article record, @Param("example") ArticleExample example);
}